package org.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springmvc.model.User;
import org.springmvc.pagination.Page;
import org.springmvc.pagination.PageRequest;
import org.springmvc.service.UserService;
import org.springmvc.util.JsonUtil;
import org.springmvc.util.MsgResponse;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户
 * @author 唐家林
 * @time 2014年9月5日
 */
@Controller
@RequestMapping(value = "/user")
public class UserController extends AbstractBaseController<User> {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/view")
    public String view(HttpServletRequest request, HttpServletResponse response) {
        return "user/user";
    }

    public String get(Integer id, HttpServletRequest request, HttpServletResponse response, String jsonCallback) {
        return null;
    }

    public String list(HttpServletRequest request, HttpServletResponse response, String jsonCallback) {
        return null;
    }

    @RequestMapping(value = "/paginate")
    @ResponseBody
    public String paginate(PageRequest pageable, HttpServletRequest request, HttpServletResponse response, String jsonCallback) {
        Page<User> users = userService.findList(pageable);
        return JsonUtil.objectToJson(users, request, jsonCallback);
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public String update(User entity, HttpServletRequest request, HttpServletResponse response, String jsonCallback) {
        MsgResponse msg = new MsgResponse();
        try {
            userService.createOrUpdate(entity);
            msg.setStatus(true).setMsg("操作成功");
        } catch (Exception e) {
            msg.setStatus(false).setMsg("操作失败");
            if (logger.isErrorEnabled()) logger.error("", e);
        }
        return JsonUtil.objectToJson(msg, jsonCallback);
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(Integer[] ids, HttpServletRequest request, HttpServletResponse response, String jsonCallback) {
        MsgResponse msg = new MsgResponse();
        try {
            userService.delete(ids);
            msg.setStatus(true).setMsg("操作成功");
        } catch (Exception e) {
            msg.setStatus(false).setMsg("操作失败");
            if (logger.isErrorEnabled()) logger.error("", e);
        }
        return JsonUtil.objectToJson(msg, jsonCallback);
    }

    /**
     * 验证登录信息
     * @param name         登录账号
     * @param password     登录密码
     * @param request      HttpServletRequest
     * @param response     HttpServletResponse
     * @param jsonCallback 用于支持跨域JSONP请求的回调函数
     * @return 返回登录结果, 已经登录或者登录成功返回true, 否则返回false
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public String login(String name, String password, HttpServletRequest request, HttpServletResponse response, String jsonCallback) {
        MsgResponse msg = new MsgResponse();
        User user = userService.getUser(name);
        if (user != null && user.getPassword().equals(password))
            request.getSession().setAttribute("USER_NAME", user.getName());
        return JsonUtil.objectToJson(msg.setStatus(user != null), jsonCallback);
    }
}
